/*
 * Copyright (c) 2012 Sean Connelly
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

var common = require('./common.js');

//
// tokens used by the lexer
//

var tokens = {
		// ! for bool, # for number, $ for string, @ for array,
		// % for object (simulating basic type checking)
		Error: ['%info'],
		Warn: ['%info'],
		Version: ['$prefix', '$chars'],
		Text: ['$chars'],
		Blob: ['$chars'],
		Ratio: ['%val', '!imag'],
		BeginText: [],
		EndText: [],
		BeginBlob: [],
		EndBlob: [],
		BeginRhotext: [],
		EndRhotext: [],
		BeginMacro: [],
		EndMacro: [],
		BeginInnerExpr: [],
		EndInnerExpr: [],
		Special: ['$chars'],
		Keyword: ['$chars'],
		Rhodent: ['$chars'],
		Ident: ['$chars'],
		CommentLine: ['$chars'],
		CommentBlock: ['$chars'],
		CommentNest: ['$chars']
	};

var genFunc = function(name, params) {
		return function(marker) {
				var obj = { tokenName: name, marker: marker };

				// inject isToken functions
				for (var k in tokens)
				{
					if (k == name)
						obj['is' + k] = function() { return true; };
					else
						obj['is' + k] = function() { return false; };
				}
				// hard-code isRident()
				obj.isRident = function() { return this.isIdent() || this.isRhodent(); };
				// add toString feature
				var args = [].splice.call(arguments, 1); // remove marker from arguments array
				obj.toStringResult = '';
				obj.toString = function() {
						return this.tokenName + (args.length > 0 ? ': ' + this.toStringResult : '');
					};

				if (params.length != args.length)
				{
					throw 'invalid call to token.' + name + '; ' +
						'expecting ' + params.length + ' parameters, instead got ' + args.length;
				}
				for (var i = 0; i < args.length; i++)
				{
					var p_type = params[i].substr(0, 1);
					var p_name = params[i].substr(1);
					if (p_type != '!' && p_type != '#' && p_type != '$' && p_type != '@' &&
						p_type  != '%')
					{
						throw 'invalid token configuration for ' + name + '; ' +
							'invalid type "' + p_type + '" for ' + p_name;
					}
					try {
						if (p_type == '!' && typeof args[i] !== 'boolean')
							throw 'boolean';
						else if (p_type == '#' && typeof args[i] !== 'number')
							throw 'number';
						else if (p_type == '$' && typeof args[i] !== 'string')
							throw 'string';
						else if (p_type == '@' && !common.isArray(args[i]))
							throw 'array';
						else if (p_type == '%' && !common.isObject(args[i]))
							throw 'object';
						obj[p_name] = args[i];
						obj.toStringResult += (obj.toStringResult == '' ? '' : ', ') + args[i];
					} catch (ex) {
						throw 'invalid call to token.' + name + '; ' +
							'expecting ' + ex + ' at parameter ' + i + ' for ' + p_name
					}
				}
				return obj;
			};
	};

for (var tokName in tokens)
	exports[tokName] = genFunc(tokName, tokens[tokName]);
